package com.waimai.mobile.ui.activity;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.waimai.mobile.R;

/**
 * Created by lin on 2015/6/13.
 */
public class BaseActivity02 extends ActionBarActivity{

    private ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        actionBar = getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.mipmap.ic_arrow_left);
        actionBar.setTitle("外卖");

    }

    public void setActionBarTitle(String title){
        actionBar.setTitle(title);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case android.R.id.home:
                onBackPressed();
                finish();
                break;
            case R.id.action_right:
                if(mListener != null){
                    mListener.onClick();
                }
                break;
            case android.R.id.title:
                Toast.makeText(getApplicationContext(), "title", Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }


    private MenuItem itemRight = null;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_02, menu);
        itemRight = menu.findItem(R.id.action_right);
        itemRight.setVisible(false);
        onOptionMenuCreated();
        return true;
    }

    public void onOptionMenuCreated(){

    }

    public void setActionRightVisible(boolean visible) {
        if(itemRight != null)
        itemRight.setVisible(visible);
    }


    public void setActionRightIcon(int iconRes){
        if(itemRight != null)
        itemRight.setIcon(iconRes);
    }

    public void setActionBarRightClickListener(ActionBarRightClickListener listener){
        mListener = listener;
    }

    private ActionBarRightClickListener mListener;
    public interface ActionBarRightClickListener{
        public void onClick();
    }
}
